<?php
/*
 * by: PhpStorm
 * 2021/7/23
 * 21:40
 * User: jweicao@qq.com
 */
namespace App\Api\Controllers\user;


use App\Api\Bases\BaseController;
use App\Api\Models\Member;
use App\Api\Models\user\CmcPointMoney;
use App\Api\Services\JsonService;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Api\Models\user\CmcPointLog as PointLog;

class CmcPointLog extends BaseController
{
    /**
     * 用户积分
     * @param Request $request
     * @return array
     */
    public function point(Request $request)
    {
        $params = $request->all();
        $validate = Validator::make($params, ['member_id'=> 'required|integer']);
        if ($validate->fails()) {
            $msg = $validate->errors()->first();
            return JsonService::fail($msg);
        }
        $filed = ['member_id','point'];
        try {
            //当前积分
            $user = Member::memberInfo($params,$filed);
            if (!$user) return JsonService::fail('未查到用户信息');
            //当日获得总积分
            $user['daySum'] = PointLog::dayPoint($params['member_id']);
            //累计总积分
            $user['sumpoint'] = PointLog::Point($params,1);
            //累计消费 2 = 消费
            $user['consume'] = PointLog::Point($params,2);
            return JsonService::success('查询成功',$user);
        }catch (\Exception $exception){
            return  JsonService::fail($exception->getMessage());
        }

    }


    public function pointMoney(Request $request)
    {
        $params = $request->all();
        $validate = Validator::make($params, [
            'member_id'=> 'required|integer',
            'money' =>'required|integer'
        ]);
        if ($validate->fails()) {
            $msg = $validate->errors()->first();
            return JsonService::fail($msg);
        }
        $where = ['member_id'=>$params['member_id']];
        //查询 用户积分
        $user = Member::memberInfo($where,'point');

        $point = $params['money']*100;
        if ($point>$user['point']) return JsonService::fail('积分不足,无法提现');
        $data = [
            'member_id' => $params['member_id'],
            'money' => $params['money'],
            'point' =>$point,
        ];
        $model = CmcPointMoney::saveData($data);
        if (!$model) return JsonService::fail('系统繁忙,请稍后再试');
        return JsonService::success('提现申请成功',$data);


    }

}
